Skip to content

Comments

feat: enhance C ABI compatibility, type inference, and JSON serialization#293

Merged
LunaStev merged 1 commit intowavefnd:masterfrom
LunaStev:feat/enhance-C-ABI-compatibility-type-inference-and-JSON-serialization
Feb 22, 2026
Merged

feat: enhance C ABI compatibility, type inference, and JSON serialization#293
LunaStev merged 1 commit intowavefnd:masterfrom
LunaStev:feat/enhance-C-ABI-compatibility-type-inference-and-JSON-serialization

Conversation

@LunaStev
Copy link
Member

This PR focuses on stabilizing the compiler's backend following the LLVM 21 (Opaque Pointer) migration. It introduces more robust type inference mechanisms, refines the C ABI lowering for complex aggregates, and adds utility functions for JSON serialization.


Key Changes

1. LLVM Backend & C ABI Refinements

  • Small Aggregate Optimization: Updated the C ABI lowering logic (abi_c.rs) to pass small "mixed" aggregates (structs with mixed types) as direct values. This allows the LLVM backend to more effectively assign values to either INTEGER or SSE registers according to the System V ABI.
  • Implicit main Return: Implemented standard-compliant behavior for the entry point. If the main function is defined without a return type in Wave, the compiler now automatically lowers it to an i32 return type in LLVM and inserts a return 0 at the end.
  • Symbol Resolution Fix: Corrected a bug where external C functions were being looked up in the LLVM symbol table using their Wave-internal names instead of their redirected C symbol names.
  • Opaque Pointer Helper: Introduced generate_address_and_type_ir. This is a critical utility for LLVM 21, as it provides both the memory address and its explicit type simultaneously, simplifying the logic for indexing and field access.

2. Advanced Type Inference

  • Recursive Inference: Added infer_wave_type_of_expr and basic_ty_to_wave_ty. These helpers allow the backend to reconstruct Wave-level type information from LLVM basic types and AST nodes, which is essential now that pointers (ptr) are ambiguous in LLVM.
  • Deref Compatibility: Updated the parser/codegen to allow redundant deref operations on already-addressable L-values. This ensures that legacy code and complex pointer arithmetic remain compatible with the stricter modular structure.

3. JSON Serialization Utilities

  • Rich Serialization: Added write_pretty_to (indented) and write_compact_to methods to the Json utility enum.
  • Escaping Logic: Implemented a comprehensive JSON string escaping engine that correctly handles newlines, quotes, backslashes, and control characters, ensuring generated manifest files are spec-compliant.

4. CLI & Optimization Flags

  • Extended Optimization: Added support for -Os (optimize for size) and -Ofast (aggressive/unsafe optimizations) in both the CLI argument parser and the LLVM PassBuilder.

5. Verification Pass

  • ABI Constraint: Added a semantic verification check to ensure that only extern(c) is used in declarations, as Wave currently focuses solely on C ABI compatibility for foreign imports.

Benefits

  • FFI Robustness: Better register allocation for structs makes C library calls faster and more reliable.
  • Opaque Pointer Stability: The new inference helpers prevent "type loss" when generating IR for LLVM 21+.
  • Developer Experience: Implicit main returns and better JSON support make the language and its toolchain easier to work with.

…tion

This commit improves the robustness of the LLVM backend regarding type
resolution under opaque pointers, refines C ABI lowering for aggregates,
and introduces JSON serialization utilities.

Changes:
- **LLVM Codegen & ABI**:
  - **C ABI Refinement**: Updated `abi_c.rs` to pass small "mixed"
    aggregates as direct values, allowing LLVM to handle register
    assignment (INTEGER vs. SSE) more effectively for the C calling
    convention.
  - **Implicit Main Return**: Implemented implicit `return 0` for the
    `main` function. If `main` is defined as void-like in Wave, it is
    now lowered to an `i32` return in LLVM to satisfy standard
    execution environments.
  - **Symbol Resolution**: Fixed a bug in `gen_function_call` where
    extern C functions were being looked up by their Wave name instead
    of their redirected LLVM symbol name.
  - **Address Generation**: Introduced `generate_address_and_type_ir`
    to provide both the memory address and its corresponding LLVM type,
    simplifying indexing and field access logic.
- **Type System & Pointers**:
  - **Enhanced Inference**: Implemented `infer_wave_type_of_expr` and
    `basic_ty_to_wave_ty` to improve Wave-to-LLVM type mapping,
    especially for complex lvalues.
  - **Legacy Compatibility**: Updated `deref` handling to allow redundant
    dereferences on already-addressable lvalues (e.g., `deref array[i]`),
    maintaining compatibility with existing code.
- **Parser & Verification**:
  - **ABI Validation**: Added a verification pass to ensure only
    `extern(c)` is used, as other ABIs are not yet supported.
- **JSON Utilities**:
  - Added `write_pretty_to` and `write_compact_to` to the `Json` enum.
  - Implemented full JSON string escaping (handles newlines, quotes,
    slashes, and control characters).
- **CLI & Flags**:
  - Added support for `-Os` (optimize for size) and `-Ofast`
    (aggressive optimization) flags in the CLI and LLVM pass builder.

These updates stabilize the transition to opaque pointers and provide
a more predictable interface for system-level FFI and serialization.

Signed-off-by: LunaStev <luna@lunastev.org>
@LunaStev LunaStev self-assigned this Feb 22, 2026
@LunaStev LunaStev merged commit b4cd414 into wavefnd:master Feb 22, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant